package xyz.lisys.utils;

import org.springframework.core.MethodParameter;
import org.springframework.web.bind.support.WebDataBinderFactory;
import org.springframework.web.context.request.NativeWebRequest;
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
import org.springframework.web.method.support.ModelAndViewContainer;
import xyz.lisys.domain.dto.CustomResolverEntity;

public class CustomArgumentResolver implements HandlerMethodArgumentResolver {
    // 将 HTTP 请求中的数据解析为控制器方法的参数

    @Override
    public boolean supportsParameter(MethodParameter parameter) {
        System.out.println(parameter.getParameterType());
        return parameter.getParameterType().equals(CustomResolverEntity.class);
    }

    @Override
    public Object resolveArgument(MethodParameter parameter, ModelAndViewContainer mavContainer, NativeWebRequest webRequest, WebDataBinderFactory binderFactory) throws Exception {
        // 当前正在解析的方法参数 | 存储模型和视图的信息 | 当前的请求对象 | 将请求参数绑定到JavaBean对象上，并支持数据校验和转换

        // ServerHttpRequest request = webRequest.getNativeRequest(ServerHttpRequest.class);

        CustomResolverEntity entity = new CustomResolverEntity();
        entity.setHeaderName("a");
        entity.setHeaderAge("b");
        entity.setBodyName("c");
        entity.setBodyAge("d");

        return entity;
    }
}
